package com.softWang.system.controller;

import com.softWang.common.response.ResponseResult;
import com.softWang.system.constants.ClientTypeEnum;
import com.softWang.system.controller.vo.systemAuth.*;
import com.softWang.system.service.SystemAuthService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

@Validated
@RestController
@Api(value="登录管理",tags="登录管理")
@RequestMapping("/system/auth")
public class SystemAuthController {
    @Resource
    private SystemAuthService authService;


    @ApiOperation(value="用户名密码登录")
    @RequestMapping(value = "/login", method = RequestMethod.POST)
    public ResponseResult<LoginResult> login(@RequestBody @Validated LoginParam param, HttpServletRequest request) throws Exception {
        param.setClientType(ClientTypeEnum.PC.getClientCode());
        LoginResult result = authService.login(param,request);
        return ResponseResult.success(result);
    }

    @ApiOperation(value="退出")
    @RequestMapping(value = "/logout", method = RequestMethod.POST)
    public ResponseResult logout(HttpServletRequest request) throws Exception {
        authService.logout(ClientTypeEnum.PC.getClientCode(),request);
        return ResponseResult.success();
    }


    @ApiOperation(value="刷新token")
    @RequestMapping(value = "/refreshToken", method = RequestMethod.POST)
    public ResponseResult<RefreshTokenResult> refreshToken(@RequestBody @Validated RefreshTokenParam param, HttpServletRequest request) throws Exception {
        param.setClientType(ClientTypeEnum.PC.getClientCode());
        RefreshTokenResult result = authService.refreshToken(param,request);
        return ResponseResult.success(result);
    }

    @ApiOperation(value="获取用户信息")
    @RequestMapping(value = "/userInfo", method = RequestMethod.POST)
    public ResponseResult<UserInfoResult> userInfo(HttpServletRequest request) throws Exception {
        UserInfoResult result = authService.userInfo();
        return ResponseResult.success(result);
    }

}
